package zookeeper.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: check-repetition
 * @description:
 * @author: hjk
 * @create: 2021-07-14 17:11
 **/
@Slf4j
@Configuration
@EnableConfigurationProperties(ZookeeperParam.class)
public class ZookeeperConfig {

    @Bean(initMethod = "start")
    public CuratorFramework zookeeperClient(ZookeeperParam zookeeperParam) {
        //重试策略，初试时间1秒，重试10次
        RetryPolicy policy = new ExponentialBackoffRetry(zookeeperParam.getBaseSleepTimeMs(), zookeeperParam.getMaxRetries());
        return CuratorFrameworkFactory.builder()
                .connectString(zookeeperParam.getServer())
                .authorization("digest",zookeeperParam.getDigest().getBytes())
                .connectionTimeoutMs(zookeeperParam.getConnectionTimeoutMs())
                .sessionTimeoutMs(zookeeperParam.getSessionTimeoutMs())
                .retryPolicy(policy).build();
    }
}
